#include <string>
#include <iostream>
#include "imDB.h"
#include <jsoncpp/json/json.h>

using namespace std;
imDB::imDB()
{

}
imDB::~imDB()
{
}
/**********************************************
 * 核对登入
 * 返回：-2绝拒，-1出错
 * ******************************************/
int imDB::join(string nickname,string pwd)
{
        int ret=-1;
	string tablename="joinUser";
        string sql="select id,nickname,password from "+tablename+" where nickname=\'"+nickname+"\' and password=md5(\'"+pwd+"\') and verified=1";
        ret=m_db.rowCount(sql);
        if(ret==1)
        {
            sql="update "+tablename+" set jointime=CURRENT_TIMESTAMP where nickname=\'"+nickname+"\'";
            m_db.ExeSQL(sql);
        }
        else
        {
            sql="select id,nickname from "+tablename+" where nickname=\'"+nickname+"\' ";
            if(m_db.rowCount(sql)>0)
                ret=-2;
        }
	return ret;
}
void imDB::reInitDB(string host,string user,string pwd,string dbname)
{
	m_db.InitDB(host,user,pwd,dbname);
}
bool imDB::regName(string nickname,string pwd)
{
    bool ret=false;
	string tablename="joinUser";
        string sql="select id,nickname from "+tablename+" where nickname=\'"+nickname+"\' ";
        if(m_db.rowCount(sql)==0)
        {
            sql="insert into "+tablename+"(nickname,password) values(\'"+nickname+"\',md5(\'"+pwd+"\'))";
            m_db.ExeSQL(sql);
            ret=true;
        }
        return ret;
}
void imDB::rePwd(string nickname,string oldpwd,string pwd)
{
	string tablename="joinUser";
        string sql="update "+tablename+" set password=md5(\'"+pwd+"\') where nickname=\'"+nickname+"\' and password=md5(\'"+oldpwd+"\')  and verified=1";
	m_db.ExeSQL(sql);
}
void imDB::creatJoinTable()
{
    string tablename="joinUser";
    string sql="CREATE TABLE "+tablename+" (";
           sql+=" `id` int(10) unsigned NOT NULL AUTO_INCREMENT,";
           sql+=" `nickname` varchar(50) DEFAULT NULL,";
           sql+=" `password` varchar(255)   DEFAULT NULL,";
           sql+=" `mood` varchar(255)   DEFAULT NULL,";
           sql+=" `jointime` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,";
           sql+=" `organ` varchar(255)   DEFAULT NULL,";
           sql+=" `name` varchar(255)   DEFAULT NULL,";
           sql+=" `mobileNumber` varchar(31)   DEFAULT NULL,";
           sql+=" `groups` varchar(255)   DEFAULT NULL,";
           sql+=" `verified` int(10) unsigned DEFAULT \'0\',";
           sql+=" PRIMARY KEY (`id`)";
          sql+=") ";
          m_db.ExeSQL(sql);
}
bool imDB::getUserArry(Json::Value &arry)
{
    string tablename="joinUser";
    string sql="select id,nickname,jointime,mood,organ,name,mobileNumber,groups,verified from "+tablename;
    return m_db.resultSQL(sql,arry);
}
bool imDB::getOrganArry(Json::Value &arry)
{
    string tablename="joinUser";
    string sql="select organ,count(id) as user_count from "+tablename+ " group by organ";
    return m_db.resultSQL(sql,arry);
}
bool imDB::getUserInfo(string nickname,Json::Value &arry)
{
    string tablename="joinUser";
        string sql="select id,nickname,jointime,mood,organ,name,mobileNumber,groups,verified from "+tablename + " where nickname=\'"+nickname+"\' ";
            return m_db.resultSQL(sql,arry);
}
void imDB::reMood(string nickname, string mood)
{
    string tablename="joinUser";
    string sql="update "+tablename+" set mood=\'"+mood+"\' where nickname=\'"+nickname+"\'";
    m_db.ExeSQL(sql);
}
void imDB::reGroups(string nickname, string groups)
{
    string tablename="joinUser";
    string sql="update "+tablename+" set groups=\'"+groups+"\' where nickname=\'"+nickname+"\'";
    m_db.ExeSQL(sql);
}
void imDB::reMobileNumber(string nickname, string mobile)
{
    string tablename="joinUser";
    string sql="update "+tablename+" set mobileNumber=\'"+mobile+"\' where nickname=\'"+nickname+"\'";
    m_db.ExeSQL(sql);
}
void imDB::reName(string nickname, string name)
{
    string tablename="joinUser";
    string sql="update "+tablename+" set name=\'"+name+"\' where nickname=\'"+nickname+"\'";
    m_db.ExeSQL(sql);
}
void imDB::reOrgan(string nickname, string organ)
{
    string tablename="joinUser";
    string sql="update "+tablename+" set organ=\'"+organ+"\' where nickname=\'"+nickname+"\'";
    m_db.ExeSQL(sql);
}
void imDB::reVerified(string nickname, bool verified)
{
    string tablename="joinUser";
    string i="0";
    if(verified) i="1";
    string sql="update "+tablename+" set verified="+i+" where nickname=\'"+nickname+"\'";
    m_db.ExeSQL(sql);
}
void imDB::delNickname(string nickname)
{
    string tablename="joinUser";
    string sql="delete from "+tablename+"  where nickname=\'"+nickname+"\'";
    m_db.ExeSQL(sql);
}
